package com.xnyzc.lhy.mis.service.impl.coupon;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.CouponException;
import com.xnyzc.lhy.common.penum.coupon.CouponErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.coupon.CouponDefParam;
import com.xnyzc.lhy.mis.entity.coupon.CouponDefSearchResult;
import com.xnyzc.lhy.mis.entity.coupon.OaSysCouponDef;
import com.xnyzc.lhy.mis.mapper.coupon.OaSysCouponDefMapper;
import com.xnyzc.lhy.mis.service.coupon.IOaSysCouponDefService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券定义 服务实现类
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-16
 */
@Service
public class OaSysCouponDefServiceImpl extends ServiceImpl<OaSysCouponDefMapper, OaSysCouponDef> implements IOaSysCouponDefService {
    @Autowired
    OaSysCouponDefMapper oaSysCouponDefMapper;

    /**
     * 获取优惠券定义（根据优惠券类型）
     *
     * @return
     */
    @Override
    public List<CouponDefSearchResult> queryCouponDefList(String defType) {
        if(StringUtils.isEmpty(defType))
        {
            throw new CouponException(CouponErrorCode.paramError);
        }

        //查询活动定义
        QueryWrapper<OaSysCouponDef> oaSysCouponDefQueryWrapper = new QueryWrapper<OaSysCouponDef>();

        //优惠券类型。00=立减劵；01=折扣卷。
        oaSysCouponDefQueryWrapper.eq(OaSysCouponDef.DEF_TYPE, defType);

        //正常记录 =0
        oaSysCouponDefQueryWrapper.eq(OaSysCouponDef.DELETED, 0);

        //查询数据
        List<OaSysCouponDef> oaSysCouponDefList = oaSysCouponDefMapper.selectList(oaSysCouponDefQueryWrapper);

        return this.createCouponDefSearchResult(oaSysCouponDefList);
    }

    /**
     * 获取优惠券定义（根据ID）
     *
     * @return
     */
    @Override
    public OaSysCouponDef queryCouponDef(String defId){
        if(StringUtils.isEmpty(defId))
        {
            return null;
        }

        //查询活动定义
        QueryWrapper<OaSysCouponDef> oaSysCouponDefQueryWrapper = new QueryWrapper<OaSysCouponDef>();

        //优惠券定义ID
        oaSysCouponDefQueryWrapper.eq(OaSysCouponDef.ID, defId);

        //正常记录 =0
        oaSysCouponDefQueryWrapper.eq(OaSysCouponDef.DELETED, 0);

        //查询数据
        return oaSysCouponDefMapper.selectOne(oaSysCouponDefQueryWrapper);
    }

    /**
     * 新增优惠券定义
     *
     * @return
     */
    @Override
    public void createCouponDef(CouponDefParam couponDefParam) {
        if(couponDefParam == null
                || StringUtils.isEmpty(couponDefParam.getDefType())
                || StringUtils.isEmpty(couponDefParam.getAmount())){
            throw new CouponException(CouponErrorCode.paramError);
        }

        //获取优惠券数据
        Date createDate = new Date();
        OaSysCouponDef oaSysCouponDef = new OaSysCouponDef();
        //优惠券类型。00=立减劵；01=折扣卷。
        oaSysCouponDef.setDefType(couponDefParam.getDefType());
        //优惠券面额
        oaSysCouponDef.setDefAmount(new BigDecimal(couponDefParam.getAmount()));

        Long defId = IDUtil.nextId();
        oaSysCouponDef.setId(defId);
        oaSysCouponDef.setCreateTime(createDate);
        oaSysCouponDef.setCreateUser(TokenUtil.getTokenUserId());
        oaSysCouponDef.setDeleted(0);
        oaSysCouponDef.setVersion(0L);
        oaSysCouponDef.setUpdateTime(createDate);

        oaSysCouponDefMapper.insert(oaSysCouponDef);
    }


    /**
     * 根据查询的优惠券定义，返回优惠券定义查询结果列表
     * @param oaSysCouponDefList
     * @return
     */
    private List<CouponDefSearchResult> createCouponDefSearchResult(List<OaSysCouponDef> oaSysCouponDefList) {
        if(oaSysCouponDefList !=null
                && oaSysCouponDefList.size() >0){
            List<CouponDefSearchResult> couponDefSearchResultList = new ArrayList<CouponDefSearchResult>(oaSysCouponDefList.size());
            for (OaSysCouponDef oaSysCouponDef : oaSysCouponDefList) {
                CouponDefSearchResult couponDefSearchResult = new CouponDefSearchResult();

                //ID
                couponDefSearchResult.setId(oaSysCouponDef.getId().toString());
                //优惠券面额
                couponDefSearchResult.setAmount(oaSysCouponDef.getDefAmount().toString());

                couponDefSearchResultList.add(couponDefSearchResult);
            }

            return couponDefSearchResultList;
        }
        else{
            return null;
        }
    }
}
